package tcu.iot.gasmanage.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tcu.iot.gasmanage.common.Result;
import tcu.iot.gasmanage.common.utils.DateUtils;
import tcu.iot.gasmanage.common.utils.EquipmentInfoResultUtils;
import tcu.iot.gasmanage.entity.Value;
import tcu.iot.gasmanage.entity.datamanagement.EquipmentNumberQuery;
import tcu.iot.gasmanage.entity.datamanagement.UpdateRealTimeQuery;
import tcu.iot.gasmanage.entity.datatable.SeriesReturn;
import tcu.iot.gasmanage.entity.equipment.EquipmentInfoResult;
import tcu.iot.gasmanage.mapper.DataTableMapper;
import tcu.iot.gasmanage.mapper.EquipmentMapper;
import tcu.iot.gasmanage.mapper.TDengineMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class DataManagementService {

    @Resource
    private EquipmentMapper equipmentMapper;
    @Resource
    private DataTableMapper dataTableMapper;

    public Result getEquipmentNumber() {
        List<Value> valuesList = dataTableMapper.FindEquipmentNumberName();

        HashMap<String, List<Value>> hashMap = new HashMap<>();
        hashMap.put("equipmentNumberOptions", valuesList);

        return Result.ok("true", 200, "成功", hashMap);
    }

    public Result UpdateData(EquipmentNumberQuery equipmentNumberQuery) {

        List<EquipmentInfoResult> list;
        if (equipmentNumberQuery.getDate() == null) {

            list = TDengineMapper.UpdateData(equipmentNumberQuery.getEquipmentNumber());

        } else {
            list = TDengineMapper.DateLineChartData(equipmentNumberQuery.getEquipmentNumber(), DateUtils.dealDateFormat(equipmentNumberQuery.getDate().get(0)), DateUtils.dealDateFormat(equipmentNumberQuery.getDate().get(1)));

        }
        return EquipmentInfoResultUtils.EquipmentInfoDataResult(list);
    }

    public Result UpdateRealTimeData(UpdateRealTimeQuery updateRealTimeQuery) {

        List<String> equipmentNumberList = new ArrayList<>();
        List<SeriesReturn> seriesReturnList = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap();

        String dataType = updateRealTimeQuery.getEquipmentNumber().get(0).get(0);

        List<String> timeQuantum = updateRealTimeQuery.getTimeQuantum();

        for (int i = 0; i < updateRealTimeQuery.getEquipmentNumber().size(); i++) {
            equipmentNumberList.add(updateRealTimeQuery.getEquipmentNumber().get(i).get(1));
        }

        if (timeQuantum == null) {
            for (String s : equipmentNumberList) {
                SeriesReturn seriesReturn = new SeriesReturn(equipmentMapper.FindEquipmentName(s), EquipmentInfoResultUtils.UpdateRealTimeResult(TDengineMapper.UpdateRealTimeDataWithoutDate(s, dataType), dataType));
                seriesReturnList.add(seriesReturn);
            }
        } else {
            for (String s : equipmentNumberList) {
                SeriesReturn seriesReturn = new SeriesReturn(equipmentMapper.FindEquipmentName(s), EquipmentInfoResultUtils.UpdateRealTimeResult(TDengineMapper.UpdateRealTimeData(s, dataType, DateUtils.dealDateFormat(timeQuantum.get(0)), DateUtils.dealDateFormat(timeQuantum.get(1))), dataType));
                seriesReturnList.add(seriesReturn);
            }
        }

        hashMap.put("series", seriesReturnList);

        return Result.ok("true", 200, "成功", hashMap);
    }
}
